package com.ethink.platform.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ethink.framework.common.constant.CommonRespCode;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.order.api.TocSaleImportLogFacade;
import com.ethink.order.api.TocSaleReturnImportLogFacade;
import com.ethink.order.api.constant.OrderRespCode;
import com.ethink.order.api.model.TocSaleReturnImportExcelModel;
import com.ethink.order.api.request.CheckShopOrderNoIfExistRequest;
import com.ethink.platform.controller.order.info.ExportInfo;
import com.ethink.platform.controller.order.info.TocSaleReturnImportDetailLogInfo;
import com.ethink.warehouse.api.SubWarehouseFacade;
import com.ethink.warehouse.api.info.SubWarehouseInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author default
 * @version 1.0.0
 * @ClassName TocSaleReturnImportManager.java
 * @Description
 * @createTime 2022年12月16日 14:57:00
 */
@Component
@Slf4j
public class TocSaleReturnImportManager {

    @Autowired
    private ExcelImportManager excelImportManager;

    @Autowired
    private TocSaleImportLogFacade tocSaleImportLogFacade;

    @Autowired
    private SubWarehouseFacade subWarehouseFacade;

    @Autowired
    private TocSaleReturnImportLogFacade tocSaleReturnImportLogFacade;

    @Autowired
    private PublicManager publicManager;

    @Resource
    private ExportManager exportManager;


    public List<TocSaleReturnImportDetailLogInfo> importTocSaleReturnLog(MultipartFile file, String saleSubChannel) {
        List<TocSaleReturnImportExcelModel> tocSaleReturnImportModels = excelImportManager.transferClass(file, TocSaleReturnImportExcelModel.class);
        List<String> shopOrderNoList = new ArrayList<>();
        // StringBuilder dumpShopOrderNos = new StringBuilder();
        List<String> repeatOrderNoList = new ArrayList<>();
        for (TocSaleReturnImportExcelModel tocSaleReturnImportModel : tocSaleReturnImportModels) {
            String trimShopOrderNo = tocSaleReturnImportModel.getShopOrderNo().trim();
            if (shopOrderNoList.contains(trimShopOrderNo)) {
                // dumpShopOrderNos.append(trimShopOrderNo + ",");
                repeatOrderNoList.add(trimShopOrderNo);
            } else {
                shopOrderNoList.add(trimShopOrderNo);
            }
        }
        // if (dumpShopOrderNos.length() > 0) {
        //     throw new BusinessException(CommonRespCode.PARAM_ERR, "网店订单号重复 " + dumpShopOrderNos);
        // }
        if (repeatOrderNoList.size() > 0) {
            // 生成文件
            ExportInfo exportInfo = exportManager.exportRepeatSaleOrderNoListExcel(repeatOrderNoList);
            throw new BusinessException(OrderRespCode.EXPORT_SALE_ORDER_NO_ERROR, "网店订单号重复 " + exportInfo.getDownloadUrl());
        }
        CheckShopOrderNoIfExistRequest checkShopOrderNoIfExistRequest = new CheckShopOrderNoIfExistRequest();
        checkShopOrderNoIfExistRequest.setSaleSubChannel(saleSubChannel);
        checkShopOrderNoIfExistRequest.setShopOrderNoList(shopOrderNoList);
        List<String> orderNoIfExist = tocSaleImportLogFacade.checkShopOrderNoIfExist(checkShopOrderNoIfExistRequest);
        List<String> diff = publicManager.getDiff(shopOrderNoList, orderNoIfExist);
        if (CollUtil.isNotEmpty(diff)) {
            // 生成文件
            ExportInfo exportInfo = exportManager.exportRepeatSaleOrderNoListExcel(diff);
            throw new BusinessException(OrderRespCode.EXPORT_SALE_ORDER_NO_ERROR, String.format("网店订单号在销售单中不存在 %s", exportInfo.getDownloadUrl()));
        }
        Map<String, SubWarehouseInfo> warehouseInfoMap = subWarehouseFacade.listSubWarehouse()
                .stream().collect(Collectors.toMap(SubWarehouseInfo::getSubWarehouseName, Function.identity()));
        List<TocSaleReturnImportDetailLogInfo> list = tocSaleReturnImportModels.stream().map(
                a -> {
                    TocSaleReturnImportDetailLogInfo info = new TocSaleReturnImportDetailLogInfo();
                    BeanUtil.copyProperties(a, info);
                    log.info("仓库名称：{}", a.getSubWarehouseName());
                    SubWarehouseInfo subWarehouseInfo = warehouseInfoMap.get(a.getSubWarehouseName().trim());
                    if (ObjectUtil.isEmpty(subWarehouseInfo)) {
                        throw new BusinessException(CommonRespCode.PARAM_ERR, "仓库名称不存在: " + a.getSubWarehouseName());
                    }
                    info.setSubWarehouseCode(subWarehouseInfo.getSubWarehouseCode());
                    info.setSubWarehouseName(subWarehouseInfo.getSubWarehouseName());
                    return info;
                }
        ).collect(Collectors.toList());
        return list;
    }
}
